package com.azunitech.apm.bundles.shell.commands;

import java.io.PrintStream;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
import org.osgi.framework.BundleException;
import com.azunitech.apm.bundles.shell.BasicCommand;

/*
 * Lists statistics about performance and connected clients.
 */
public class CommandSTRT extends BasicCommand<BundleContext>
{	
	private static final String TAG = CommandSTRT.class.getSimpleName();
	private static final String CMD_KEY = "strt";
	private static final String CMD_DESC = "start a bundle with bundle id.";

	public String getKey() 
	{
		return CommandSTRT.CMD_KEY;
	}

	public String getDesc() 
	{
		return CommandSTRT.CMD_DESC;
	}

	public void exec(String args, PrintStream out, PrintStream err)
			throws Exception 
	{
		String str = args.substring(5).trim();
		BundleContext context = (BundleContext)this.m_target;
		new SelfStrtThread(context.getBundle( Long.parseLong(str))).start();		
	}
	
	private static final class SelfStrtThread extends Thread 
	{
	    private final Bundle m_self;

	    public SelfStrtThread(Bundle self) 
	    {
	    	super("SelfStopThread Bundle " + self.getBundleId());
	    	m_self = self;
	    }

	    public void run() 
	    {
	    	try 
	    	{
	    		m_self.start();
	    	} 
	    	catch (BundleException e){
	    		e.printStackTrace();
	    	}
	    }
	 }
}
